<template>
  <div class="news" :style="{height: height}">
    <!-- title是给子组件传递的值 -->
    <Navbar title="新闻列表"></Navbar>

    <div class="new-list">
      <ul>
        <li v-for="item in newsData" :key="item.id">
          <!-- 使用命名路由跳转 传递news id-->
          <router-link :to="{name: 'news-detail', query: {id: item.id}}">
            <div class="new-img">
              <img :src="item.img_url" alt />
            </div>
            <div class="content">
              <p class="title">{{ item.title }}</p>
              <div class="new-desc">
                <p class="summary">{{ item.summary }}</p>
                <p>
                  <span class="praise">{{ item.like }}赞</span>
                  <span class="praise">{{ item.fav }}收藏</span>
                  <span class="praise">{{ item.view }}浏览</span>
                  <span class="time">发布时间：{{ item.create_time | timeFormat('YYYY-MM-DD') }}</span>
                </p>
              </div>
            </div>
          </router-link>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: "NewsList",
  data() {
    return {
      // 新闻数据
      newsData: [],
      height: document.documentElement.clientHeight - 95 + 'px'
    };
  },
  methods: {
    // 获取所有新闻数据
    getNewsList() {
      this.$http
        .get("/news")
        .then(res => {
          this.newsData = res.data.news;
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  created() {
    this.getNewsList();
    console.log(document.documentElement.clientHeight)
  }
};
</script>

<style lang="less" scoped>
.news {
  //   padding-bottom: 100px;
  margin-bottom: 40px;
  margin-top: 40px;
  overflow: hidden;
  overflow-y: auto;
}
.new-list {
  width: 100%;
  border-bottom: 1px solid #cccccc;
  font-size: 12px;
}
.new-list ul li {
  position: relative;
}
.new-list ul li a {
  display: block;
  width: 100%;
  height: 150px;
  color: #808080;
  display: flex;
  text-decoration: none;
}
.new-img {
  width: 30%;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px 0 0;
}
.new-img img {
  width: 100px;
  height: 80px;
  background-color: red;
  border-radius: 3px;
}
.content .title {
  font-size: 15px;
  display: inline-block;
  color: #131f3c;
  letter-spacing: 0;
  padding-bottom: 7px;
  font-family: PingFangSC-Regular;
  margin-top: 10px;
}
.content .new-desc {
  margin-top: 20px;
}
.new-desc span {
  color: #ff6700;
}
.summary {
  padding: 5px 0px;
}
.time {
  margin-left: 30px;
}
</style>
